package net.recursv.motific.atweaver

import org.apache.tools.ant.BuildException
import org.apache.tools.ant.Task

/**
 * Created by: recursv
 * Date: 14/08/2009
 * Time: 2:14:29 PM
 */

public class AtWeaverTask extends Task {

  protected List<File> _input = new ArrayList<File>();

  protected File _output;

  protected Boolean _overwriteOutput = true;

  protected Boolean _cleanOutput = true;

  protected Boolean _verbose = false;

  /**
   * Runs the code generators.
   */
  public void execute() throws BuildException {
    FileParser parser = new FileParser()
    parser.input.addAll(_input)
    parser.output = _output.absoluteFile
    parser.overwrite = _overwriteOutput
    parser.cleanOutput = _cleanOutput
    parser.verbose = _verbose;

    if (parser.validate()) {
      parser.run()
    } else {
      throw new BuildException(usage);
    }
  }

  /**
   * Sets the source files
   *
   * @param input The source
   */
  public void setInput(File input) {
    _input.add(input)
  }

  /**
   * Sets the output destination
   *
   * @param output the destination
   */
  public void setOutput(File output) {
    _output = output;
  }

  /**
   * @param b true if you want to overwrite existing files.
   */
  public void setOverwrite(Boolean b) {
    _overwriteOutput = b;
  }

  /**
   * @param b true if you want to empty the output directory before generating new content
   */
  public void setClean(Boolean b) {
    _cleanOutput = b;
  }

  /**
   * @param b true if you want lots of output
   */
  public void setVerbose(Boolean b) {
    _verbose = b;
  }

  String usage = '<AtWeaverTask input="<source directory>" output="<output directory>" [-overwrite="true|false"] [-clean="true|false"] [-verbose="true|false"]  />'

  public void setSource(String path) {
    getProject().getReference(path).each {_input.add(it.getFile())}
  }
}
